<template>
   <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="地址名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('handle.search') }}</el-button>
        <el-button type="primary" @click="addOrUpdate()">{{ $t('handle.add') }}</el-button>
        <!-- <el-button type="danger" @click="deleteAddress()" :disabled="dataListSelections.length <= 0">{{ $t('handle.batchDelete') }}</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      height="500"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      ref="table"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        min-width="200px"
        :show-overflow-tooltip = true
        label="地址名称">
        <template slot-scope="scope">
          {{scope.row.region.pnames}}, {{scope.row.region.name}} , {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column
        prop="policeDistrictCode"
        header-align="center"
        align="center"
        label="警务责任区代码">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="登记人姓名">
        <template slot-scope="scope">
          <i v-if="scope.row.registrant" >{{scope.row.registrant.name}}</i>
        </template>
      </el-table-column>
      <el-table-column
        prop="registrationTime"
        header-align="center"
        align="center"
        :formatter="dateFormat"
        label="登记时间">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="登记单位">
        <template slot-scope="scope">
          <i v-if="scope.row.applicant" >{{scope.row.applicant.name}}</i>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="allowedToSettle"
        header-align="center"
        align="center"
        label="允许落户判断标识">
      </el-table-column>
      <el-table-column
        prop="goodLive"
        header-align="center"
        align="center"
        label="适合居住判断标识">
      </el-table-column>
      <el-table-column
        prop="addressPresence"
        header-align="center"
        align="center"
        label="地址存在标识">
      </el-table-column>
      <el-table-column
        prop="addressIdentified"
        header-align="center"
        align="center"
        label="地址在用标识">
      </el-table-column> -->
      <!-- <el-table-column
        prop="cancellationTime"
        header-align="center"
        align="center"
        label="撤销时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="更新时间">
      </el-table-column>
      <el-table-column
        prop="openingDate"
        header-align="center"
        align="center"
        label="启用日期">
      </el-table-column>
      <el-table-column
        prop="deactivationDate"
        header-align="center"
        align="center"
        label="停用日期">
      </el-table-column> -->
      <!-- <el-table-column
        prop="longitude"
        header-align="center"
        align="center"
        label="坐标经度">
      </el-table-column>
      <el-table-column
        prop="latitude"
        header-align="center"
        align="center"
        label="坐标纬度">
      </el-table-column> -->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        :label="$t('handle.handle')">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdate(scope.row.id,true)">{{ $t('handle.detail') }}</el-button>
          <el-button type="text" size="small" @click="addOrUpdate(scope.row.id)">{{ $t('handle.update') }}</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('handle.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './add-or-update.vue'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      // this.getDataList()
    },
    computed: {
      regionId() {
        return this.$store.state.address.regionId;
      }
    },
    methods: {
      dateFormat:function(row, column) {
          var date = row.registrationTime;
          if (date == undefined) {
             return "";
          }
          return this.$moment(date).format("YYYY-MM-DD");
      },
      adjust(){
        setTimeout(()=>{
          this.$refs['table'].doLayout()
        },100)
      },
      // 获取数据列表
      getAllAdressName(row, column, cellValue, index){
            return row.name;
      },
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: '/dg-standard-info/dgstandardaddress/list',
          method: 'post',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            'regionId':this.regionId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        },
          error => {
            if (error.response.status === 403) {
              this.$message.error("你没有权限");
            } else {
              this.$message.error("系统错误，请联系管理员");
            }
          })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdate (id,detail) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,detail)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: `/dg-standard-info/dgstandardaddress/deleteaddress/${id}`,
            method: 'delete',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style>
#pane-second{
  height: 100%;
  overflow: auto;
}
</style>

